package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.domain.Movie;
import com.ruoyi.system.domain.vo.MovieVo;
import com.ruoyi.system.mapper.MovieMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.PublisherBo;
import com.ruoyi.system.domain.vo.PublisherVo;
import com.ruoyi.system.domain.Publisher;
import com.ruoyi.system.mapper.PublisherMapper;
import com.ruoyi.system.service.IPublisherService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-04
 */
@RequiredArgsConstructor
@Service
public class PublisherServiceImpl implements IPublisherService {

    private final PublisherMapper baseMapper;

    private final MovieMapper movieMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public PublisherVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<PublisherVo> queryPageList(PublisherBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Publisher> lqw = buildQueryWrapper(bo);
        Page<PublisherVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<PublisherVo> queryList(PublisherBo bo) {
        LambdaQueryWrapper<Publisher> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Publisher> buildQueryWrapper(PublisherBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Publisher> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), Publisher::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getCompany()), Publisher::getCompany, bo.getCompany());
        lqw.eq(bo.getTime() != null, Publisher::getTime, bo.getTime());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(PublisherBo bo) {
        Publisher add = BeanUtil.toBean(bo, Publisher.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(PublisherBo bo) {
        Publisher update = BeanUtil.toBean(bo, Publisher.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Publisher entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public PublisherVo getInfoByID(Long id) {
        PublisherVo result = new PublisherVo();
        Publisher publisher = baseMapper.selectById(id);
        BeanUtil.copyProperties(publisher, result);
        QueryWrapper<Movie> wrapper = new QueryWrapper<>();
        wrapper.eq("publisher_id", id);
        List<Movie> movies = movieMapper.selectList(wrapper);
        result.setMovieVoList(movies);
        return result;
    }

    @Override
    public boolean createPublisher(PublisherBo bo) {
        Publisher publisher = new Publisher();
        BeanUtil.copyProperties(bo, publisher);
        baseMapper.insert(publisher);
        return true;
    }
}
